require(["jquery","bbcare",
        "scprit/department/vm/assessmentScreening/bsid2.ui.js",
        "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js",
        "scprit/department/vm/assessmentScreening/bsid2.event.js"],
    function ($,$bb,$ui,$servers,$event) {
        var getParams = $bb.getPageParams();
        var type = getParams.type;
        var userId = getParams.userId;
        var answerId = getParams.answerId;
        var scoreObj;
        //问卷题目获取：
        var paramsQuestion = $servers.getParams(type,userId);
        //患者信息展示：
        var infoParams = $servers.infoParamsBsid2(userId,type);
        var infoFn = function(dt){
            //基本信息渲染：
            dt = dt.data;
            var answerResult ;
            var diff,sports,intellgence,sportsAge,intellgenceAge,intellgenceStart,intellgenceEnd,sportsStart,sportsEnd,isEnd;
            if(answerId){
                answerResult = JSON.parse(getParams.answer.answerResult);
                diff = $servers.dateDiff(dt.PatientInfo.birthday,getParams.testTime);
                dt.PatientInfo.testDate = getParams.testTime;
                dt.PatientInfo.height = answerResult.height;
                dt.PatientInfo.heightFlag = 1;
                isEnd = "end";
                intellgenceStart = answerResult.ques.start.intellgence;
                intellgenceEnd = answerResult.ques.end.intellgence;
                sportsStart = answerResult.ques.start.sports;
                sportsEnd = answerResult.ques.end.sports;
                scoreObj = answerResult.ques;
                sports = answerResult.score.prScore;
                intellgence = answerResult.score.prScore;
            }else{
                diff = $servers.dateDiff(dt.PatientInfo.birthday,$servers.getNowFormatDate());
                dt.PatientInfo.testDate = $servers.getNowFormatDate();
                scoreObj = {"intellgence":{},"sports":{},"behavior":{},"start":{},"end":{}};
            }
            //计算月龄
            var monthAge = $servers.getMonth(diff,dt.PatientInfo.pregnancyDay);
            dt.PatientInfo.isPremature = monthAge.flag;
            dt.PatientInfo.monthAge = monthAge.monthAge;
            //计算智龄：
            if(dt.Evaluation && typeof dt.Evaluation.score  == 'object'){
                var lastTime = dt.Evaluation.testTime;
                diff = $servers.dateDiff(lastTime,$servers.getNowFormatDate());//计算上一次测试距离当前测试的天数；
                var score = JSON.parse(dt.Evaluation.score);
                var prAge = score.PR.intelligenceAge,mrAge = score.MR.intelligenceAge;
                sportsAge = $event.intelligenceAge(prAge,diff);
                intellgenceAge = $event.intelligenceAge(mrAge,diff);
            }else{
                sportsAge = intellgenceAge =monthAge.monthAge;
            }
            dt.PatientInfo.intelligenceAge = intellgenceAge;//智力智龄；
            dt.PatientInfo.sportsAge = sportsAge;//运动智龄；
            $ui.basicInfoInit({"id":"#basic-info","data":dt.PatientInfo});
            var quesFn = function(data){
                $ui.tabInit({
                        "id":"#tabs",
                        "data":{
                            "sports":sports,
                            "intellgence":intellgence,
                            "intellgenceAge":intellgenceAge,
                            "sportsAge":sportsAge,
                            'intellgenceStart':intellgenceStart,
                            "intellgenceEnd":intellgenceEnd,
                            "sportsStart":sportsStart,
                            "sportsEnd":sportsEnd,
                            "isEnd":isEnd
                        }
                });
                var data = $event.dataDeal(data.data,scoreObj);
                var stageData = $event.stageList(data,scoreObj);
                var monthData = $event.monthList(data,scoreObj);
                $event.tabClick(data,stageData,monthData,scoreObj);
                $("#tabs>div").eq(0).click($event.tabCallback("#tabs>div.selected-tab",data,stageData,monthData,scoreObj));
                if(!answerId){
                    $event.getScoreClick(scoreObj,userId,type);
                }else{
                    var viewResult = function(){
                        $bb.openPage("bsidResult2",{
                            "userId":userId,
                            "answerId":answerId,
                            "type":type
                        });
                    };
                    var topbars = [
                        {
                            "icon":"fa fa-eye",
                            "name":"查看结果",
                            "callback":viewResult
                        }
                    ];
                    $bb.toolbar(topbars);
                }
            };
            $servers.getQuestion(paramsQuestion,quesFn);
        };
        $servers.getInfo(infoParams,infoFn);
    });
